from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
from ui_auto import HTMLTestRunner3_New
import unittest


class Discuz(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
# 自动登录
            cls.driver=webdriver.Chrome()
            cls.driver.get("http://192.168.0.62/bbs/forum.php")
            cls.driver.maximize_window()
            cls.driver.find_element(By.ID,"ls_username").send_keys("admin")
            cls.driver.find_element(By.ID,"ls_password").send_keys("123456")
            cls.driver.find_element(By.XPATH,'//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[3]/button').click()
    def test001_send(self):
# 自动发帖
            sleep(2)
            mouse=self.driver.find_element(By.LINK_TEXT,"我的")
            a=ActionChains(self.driver)
            a.move_to_element(mouse).perform()
            sleep(1)
            self.driver.find_element(By.LINK_TEXT,"帖子").click()
            self.driver.implicitly_wait(20)
            self.driver.find_element(By.XPATH,'/html/body/div[7]/div[2]/div/div/div[2]/a/img').click()
            sleep(1)
            self.driver.find_element(By.XPATH,'/html/body/div[1]/div/table/tbody/tr[2]/td[2]/div[2]/ul/li[1]/p[2]/a').click()
            sleep(1)
            self.driver.find_element(By.XPATH,'//*[@id="block_forum"]/p/a').click()
            sleep(3)
            self.driver.find_element(By.XPATH,'//*[@id="postbtn"]').click()
            sleep(3)
            self.driver.find_element(By.XPATH,'//*[@id="subject"]').send_keys("再来一次")
            self.driver.switch_to.frame("e_iframe")
            sleep(2)
            self.driver.find_element(By.XPATH,"/html/body").send_keys("我要打10个")
            self.driver.switch_to.default_content()
            self.driver.find_element(By.XPATH,'//*[@id="postsubmit"]').click()
    def test002_delete(self):
#自动删帖
            sleep(2)
            self.driver.find_element(By.XPATH,'//*[@id="hd"]/div/div[1]/h2/a/img').click()
            mouse1=self.driver.find_element(By.LINK_TEXT,"我的")
            a=ActionChains(self.driver)
            a.move_to_element(mouse1).perform()
            self.driver.find_element(By.LINK_TEXT,'帖子').click()
            self.driver.find_element(By.XPATH,'/html/body/div[7]/div[2]/div/div/div[3]/div[2]/table/tbody[1]/tr/th/a').click()
            handles=self.driver.window_handles
            print(handles)
            self.driver.switch_to.window(handles[1])
            sleep(1)
            self.driver.find_element(By.LINK_TEXT,'删除主题').click()
            self.driver.find_element(By.XPATH,'//*[@id="moderateform"]/div/div[2]/h4/a').click()
            self.driver.find_element(By.LINK_TEXT,'违规内容').click()
            self.driver.find_element(By.XPATH,'/html/body/div[1]/div[2]/table/tbody/tr[2]/td[2]/div/form/p/button').click()
    @classmethod
    def tearDownClass(cls) -> None:
           cls.driver.quit()


if __name__ == '__main__':
    discover=unittest.defaultTestLoader.discover(start_dir=r"D:\python--tl\ui_auto",pattern="discuz_ui_auto.py")
    file=open(r"D:\python--tl\ui_auto\Discuzreport.html","wb")
    runner=HTMLTestRunner3_New.HTMLTestRunner(stream=file,title="Discuz自动登录发帖删帖测试报告",description="用例执行情况如下",tester="TL")
    runner.run(discover)



# driver.close()













